﻿using AK56.Domain.Models;
using AK56.Domain.PubilcEnums;
using AK56.Domain.Services;
using AK56.Domain.Untils;
using AKCY.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Web.Http.Filters;

namespace AK56.APP.Application.Home.Controllers
{
    public class DashBoardController : ApiController
    {
        [HttpPost]
        public HttpResponseMessage GetStatisticsData(int? branch, int? node)
        {
            var ctx = AKCY.Infrastructure.ServiceLocator.GetServiceByName<AKCY.Infrastructure.EntityFramework.IEntityFrameworkRepositoryContext>("ak56_lip_ef").Context;
            DateTime starttime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
            DateTime endtime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));

            #region 今日收货量

            var todayInsql = @"SELECT ISNULL(SUM(ISNULL(PCD_Quan,0)),0)                                
                               FROM [PC_StowageMaster] pcsm,[PC_StowageDetail] pcsd
                               where pcsd.[PCD_PCM_ID]=pcsm.PCM_ID and pcsm.PCM_Type=1 and pcsm.PCM_Status=2
                               and pcsm.PCM_Arrive_Time>='" + starttime + "' and pcsm.PCM_Arrive_Time<='" + endtime + @"'
                               ";
            if (branch != null)
            {
                todayInsql += " and pcsm.PCM_To_Branch=" + branch + " ";
            }
            if (node != null)
            {
                todayInsql += " and pcsm.PCM_To_Node=" + node + "";
            }

            #endregion

            #region 今日落货量

            var todayUnloadsql = @"SELECT ISNULL(SUM(ISNULL(PCD_Quan,0)),0)
                                  FROM [PC_StowageMaster] pcsm,[PC_StowageDetail] pcsd,JOB_TransportOrder jobto
                                  where pcsd.[PCD_PCM_ID]=pcsm.PCM_ID and pcsd.PCD_JTO_ID=jobto.JTO_ID and pcsm.PCM_Type=1 and pcsm.PCM_Status=2
                                  and jobto.JTO_Dest_Branch=pcsm.PCM_To_Branch and jobto.JTO_Dest_Node=pcsm.PCM_To_Node
                                  and pcsm.PCM_Arrive_Time>='" + starttime + "' and pcsm.PCM_Arrive_Time<='" + endtime + @"'
                                  ";
            if (branch != null)
            {
                todayUnloadsql += " and pcsm.PCM_To_Branch=" + branch + " ";
            }
            if (node != null)
            {
                todayUnloadsql += " and pcsm.PCM_To_Node=" + node + "";
            }

            #endregion

            #region 发货库存统计

            var querywhinsql = @"SELECT  ISNULL(SUM(ISNULL(PCD_Quan,0)),0)                                
                               FROM [PC_StowageMaster] pcsm,[PC_StowageDetail] pcsd
                               where pcsd.[PCD_PCM_ID]=pcsm.PCM_ID and pcsm.PCM_Type=1 and pcsm.PCM_Status=2
                               ";
            var whincountsql = SqlToolkit.GetRowCountSql(querywhinsql.ToString()).FirstOrDefault();
            if (branch != null)
            {
                querywhinsql += " and pcsm.PCM_To_Branch=" + branch + " ";
            }
            if (node != null)
            {
                querywhinsql += " and pcsm.PCM_To_Node=" + node + "";
            }


            #endregion

            #region 落货库存统计

            string whoutsql = @"SELECT ISNULL(SUM(ISNULL(PCD_Quan,0)),0)
                                  FROM [PC_StowageMaster] pcsm,[PC_StowageDetail] pcsd,JOB_TransportOrder jobto
                                  where pcsd.[PCD_PCM_ID]=pcsm.PCM_ID and pcsd.PCD_JTO_ID=jobto.JTO_ID and pcsm.PCM_Type=1 and pcsm.PCM_Status=2
                                  and jobto.JTO_Dest_Branch=pcsm.PCM_To_Branch and jobto.JTO_Dest_Node=pcsm.PCM_To_Node
                                 ";
            if (branch != null)
            {
                whoutsql += " and pcsm.PCM_To_Branch=" + branch + " ";
            }
            if (node != null)
            {
                whoutsql += " and pcsm.PCM_To_Node=" + node + "";
            }

            #endregion

            #region 待处理异常


            string queryPendingExpsql = @"SELECT count(*)
                                            FROM  [dbo].[JOB_Exceptions]
                                            INNER JOIN [dbo].[JOB_TransportOrder] ON [JE_JTO_ID] = [JTO_ID]
                                            WHERE  CAST([JE_Status] AS int)=" + (int)AK56.Domain.PubilcEnums.ExceptionStatus.UnDisposed + @"";
                                 
            if (branch != null)
            {
                queryPendingExpsql += " and JE_Handle_Branch=" + branch + " ";
            }
            if (node != null)
            {
                queryPendingExpsql += " and JE_Handle_Node=" + node + "";
            }

            #endregion

            var todayIn = ctx.Database.SqlQuery<int?>(todayInsql.ToString()).FirstOrDefault() == null ? 0 : ctx.Database.SqlQuery<int?>(todayInsql.ToString()).FirstOrDefault();
            var todayUnload = ctx.Database.SqlQuery<int?>(todayUnloadsql.ToString()).FirstOrDefault()== null ? 0 : ctx.Database.SqlQuery<int?>(todayUnloadsql.ToString()).FirstOrDefault();
            var whinCount = ctx.Database.SqlQuery<int?>(querywhinsql.ToString()).FirstOrDefault()== null ? 0 : ctx.Database.SqlQuery<int?>(querywhinsql.ToString()).FirstOrDefault();
            var whoutCount = ctx.Database.SqlQuery<int?>(whoutsql.ToString()).FirstOrDefault() == null ? 0 : ctx.Database.SqlQuery<int?>(whoutsql.ToString()).FirstOrDefault();
            var pendingExp = ctx.Database.SqlQuery<int?>(queryPendingExpsql.ToString()).FirstOrDefault() == null ? 0 : ctx.Database.SqlQuery<int?>(queryPendingExpsql.ToString()).FirstOrDefault();
            return Request.CreateResponse(HttpStatusCode.OK, new { todayIn = todayIn, todayUnload = todayUnload, whinCount = whinCount, whoutCount = whoutCount, pendingExp = pendingExp });
        }
    }
}
